﻿layui.use(['table', 'laypage', 'layer', 'form'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer; // 添加layui弹出层模块

    var pageSize = 15; // 每页显示条数
    //获取列表
    function getListData(pageIndex) {
        //获取查询条件
        var RoleName = $('#RoleName').val();
        //ajax后台交互
        $.ajax({
            //http://localhost:52691/api/Permission/ShowPagePermission?pageIndex=1&pageSize=10
            url: 'http://10.31.60.18:8000/AuthorizationPeopleGetRout/api/Permission/ShowPagePermission',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                FunctionName: $("#functionName").val(),
            },
            success: function (res) {
                //渲染表格数据
                layui.use(['table', 'laypage'], function () {
                    var table = layui.table;
                    var laypage = layui.laypage;

                    table.render({
                        //挂载的表格id
                        elem: '#ID-table-demo-setRowChecked',
                        cols: [[
                            { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                            { field: 'functionName', title: '功能', width: 120, align: 'center' },
                            { field: 'operation', title: '操作', width: 120, align: 'center' },
                            { field: 'url', title: '路径', width: 120, align: 'center' },
                            { field: 'roleName', title: '角色', width: 120, align: 'center' },
                            { title: '操作', width: 120, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },

                        ]],
                        data: res.data
                    });

                    //分页
                    laypage.render({
                        //挂载的分页id
                        elem: 'pagesbox',
                        count: res.totalCount,
                        limit: pageSize,
                        curr: pageIndex,
                        layout: ['count', 'prev', 'page', 'next', 'skip'],
                        jump: function (obj, first) {
                            if (!first) {
                                getListData(obj.curr);
                            }
                        }
                    });
                });
            }
        });
    }

    $('#searchBtn').on('click', function () {
        getListData(1);
    });

    $(function () {
        // 初始化加载数据
        getListData(1);
    })

    // 监听表单提交
    form.on('submit(save)', function (data) {
        var formData = {
            RoleName: $("#roleName").val(),
            RoleRemark: $("#roleRemark").val(),
            // NewEarTag: $("input[name='newEarTag']").val(),
            // Reason: $("#reasonsAdd").val(),

            CreateBy: "张三",
            CreateDate: Date.now,
        };

        $.ajax({
            url: 'http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Employee/AddRole', // 替换为您的后端API接口地址
            type: 'post',
            data: JSON.stringify(formData),
            contentType: 'application/json',
            success: function (res) {
                if (res > 0) {
                    alert("添加成功！");
                    location.href = '/Role/ShowRole'
                    // layer.msg(res.msg, {icon: 1 });
                    // setTimeout(function () {
                    //     location.href = '/Role/ShowRole'
                    // }, 1000)
                } else {
                    //layer.msg(res.msg, { icon: 2 });
                    alert("添加失败！");
                }
            },
            error: function () {
                layer.msg('网络错误，请稍后重试', { icon: 2 });
            }
        });
        return false; // 阻止表单跳转
    });

    // 重置按钮点击事件
    $('#closeBtn').on('click', function () {
        layer.closeAll();
    });

    // 单删除
    function deleteData(permissionId) {
        $.ajax({
            url: `http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Permission/FalseDelete?permissionId=${permissionId}`,
            type: 'delete',
            success: function (res) {
                if (res > 0) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除失败');
                    getListData(1);
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }

    // 监听工具条，实现删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.permissionId); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });

    // 批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
            data = checkStatus.data,
            permissionId = "";
        if (data.length > 0) {
            for (var i in data) {
                permissionId += data[i].permissionId + ",";
            }
            permissionId = permissionId.substring(0, permissionId.length - 1);
            console.log(permissionId);
            layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: `http://10.31.60.18:8000/AuthorizationPeopleSetRout/api/Permission/AllFalseDelete?permissionId=${permissionId}`,
                    type: 'put',
                    success: function (res) {
                        if (res > 0) {
                            layer.msg("批量删除成功");
                            getListData(1);
                        } else {
                            layer.msg("批量删除失败");
                        }
                    },
                    error: function () {
                        layer.msg('批量删除请求失败');
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg("请选择需要删除的记录");
        }
        console.log(checkStatus);
    });
});

var $ = layui.jquery;
//添加的弹出层信息
function onAddBtn() {
    var laydate = layui.laydate;
    // 渲染
    laydate.render({
        elem: '#storageDate'
    });

    layer.open({
        type: 1,
        title: "添加角色信息",
        closeBtn: false,
        shift: 12,
        area: ['500px', '300px'],
        shade: 0,
        shadeClose: false,
        content: $("#add-main"),
        success: function (layero, index) {
        }
    });
}